﻿PermiasCongress = {}

$(document).ready(function () {
    PermiasCongress.init();
});

PermiasCongress = {
    init: function () {
        PermiasCongress.util.initAccordion();
        PermiasCongress.util.initGalleryControl();
        PermiasCongress.util.initVideoControl();
        PermiasCongress.util.showModalImage();
    }
}


PermiasCongress.util = {
    initAccordion: function () {
        $("#accordion").accordion({
            collapsible: true,
            heightStyle: "content",
            active: false,
            autoHeight: false
        });
    },
    initGalleryControl: function () {
        $(".scrollLeft").click(function (e) {
            e.preventDefault();
            PermiasCongress.util.scrollGalleryLeft();
        });
        $(".scrollRight").click(function (e) {
            e.preventDefault();
            PermiasCongress.util.scrollGalleryRight();
        });
    },
    scrollGalleryLeft: function () {
        if (parseInt($(".photoGalleryContent").css("left")) == 0 || $(".photoGalleryContent").is(":animated")) {
            //do nothing
        } else {
            $('.photoGalleryContent').stop(true, true).animate({
                left: '+=1000'
            }, 1000, function () {
                // Animation complete.
            });
        }
    },
    scrollGalleryRight: function () {
        if (parseInt($(".photoGalleryContent").css("left")) == -2000 || $(".photoGalleryContent").is(":animated")) {
            //do nothing
        } else {
            $('.photoGalleryContent').stop(true, true).animate({
                left: '-=1000'
            }, 1000, function () {
                // Animation complete.
            });
        }
    },
    initVideoControl: function () {
        var left = 0;
        $(".video").each(function (i) {
            $(this).css("left", left);
            left += 360;
        });

        $(".prevVideo").live("click", function (e) {
            e.preventDefault();
            PermiasCongress.util.getPrevVideo();
        });
        $(".nextVideo").live("click", function (e) {
            e.preventDefault();
            PermiasCongress.util.getNextVideo();
        });
    },
    getNextVideo: function () {
        if (parseInt($(".video:last").css("left")) == 0 || $(".video").is(":animated")) {
            //do nothing
        } else {
            $(".video").each(function () {
                $(this).animate({
                    left: '-=360'
                }, 1000, function () {
                    // Animation complete.
                });
            });

        }
    },
    getPrevVideo: function () {
        if (parseInt($(".video:first").css("left")) == 0 || $(".video").is(":animated")) {
            //do nothing
        } else {
            $(".video").each(function () {
                $(this).animate({
                    left: '+=360'
                }, 1000, function () {
                    // Animation complete.
                });
            });

        }
    },
    showModalImage: function () {

        $(".photoGalleryContent a").click(function (e) {
            var containerHeight = $(window).height();
            var containerWidth = $(window).width();
            e.preventDefault();
            var src = $(this).find("img").attr("src");
            $("body").append("<div class='modalOverlay'></div>");
            $("body").append("<div class='overlay'></div>");
            $(".modalOverlay").html("<a href='#'>x</a>");
            $(".modalOverlay").append("<img style='display:none;' src='#' />");
            $(".modalOverlay img").attr("src", src);
            $(".modalOverlay img").css({
                "max-height": ($(window).height() - 80),
                "max-width": ($(window).width() - 80)
            });
            var img = new Image();
            var height, width;
            img.onload = function () {
                width = this.width;
                height = this.height;

                var newWidth = $(".modalOverlay img").width();
                var newHeight = $(".modalOverlay img").height();

                $(".modalOverlay img").css({
                    "margin-top": (newHeight / 2) * -1,
                    "margin-left": (newWidth / 2) * -1
                });
                $(".modalOverlay img").fadeIn(1000);
            }

            img.src = src;

        });
        PermiasCongress.util.closeModalImage();
    },
    closeModalImage: function () {
        $(".modalOverlay").live("click", function (e) {
            $(".overlay").remove();
            $(".modalOverlay").remove();
        });
    },
    loadGalleryImage: function () {
        $(".photoGalleryContent a").each(function (i) {
            var getImage = $(this).find("img");
            var this_image = getImage[0];
            var src = $(this_image).attr('src');
            var lsrc = $(this_image).attr('lsrc');
            if (lsrc.length > 0) {
                var img = new Image();
                img.src = lsrc;
                $(img).load(function () {
                    this_image.src = this.src;
                });
            }
            
        });
    }
}